//
//  DemoTableView109ViewController.m
//  DemoTableView109
//
//  Created by yang on 10-10-8.
//  Copyright 2010 www.1000phone.com. All rights reserved.
//

#import "DemoTableView109ViewController.h"

@implementation DemoTableView109ViewController

@synthesize myTableView;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	self.myTableView = nil;
    [super dealloc];
}

- (NSInteger) tableView:(UITableView *)tableView
			numberOfRowsInSection:(NSInteger)section
{
	return 200;
}
- (UITableViewCell *) tableView:(UITableView *)tableView
			cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	int row = [indexPath row];
	UITableViewCell *cell = nil;
	if (row % 2 == 0) {
		cell = [tableView
				dequeueReusableCellWithIdentifier:@"MyBookCell"];
		if (cell == nil) {
			cell = [[[NSBundle mainBundle] 
					 loadNibNamed:@"MyBookCell"
						owner:self options:nil] lastObject];
		}
		
		UILabel *label = (UILabel *)[cell viewWithTag:101];
		/* 因为UILabel是UITableViewCell的孩子 
		 并且UILabel设置的tag为101 这里通过cell里面去查找
		 tag 为101的孩子
		 */
		label.text = [NSString stringWithFormat:@"第%d章", row+1];
		
		UISwitch *mySwitch = (UISwitch *)[cell viewWithTag:102];
#if 0
		if (row %2 == 0) {
			[mySwitch setOn:YES];
		} else {
			[mySwitch setOn:NO];
		}
#endif
		[mySwitch addTarget:self action:@selector(switchChanged:) 
		   forControlEvents:UIControlEventValueChanged];
		/* 
		 addTarget:action:forControlEvents:
		 作用是给一个空间设置一个事件函数
		 上面给mySwitch增加一个 UIControlEventValueChanged事件
		 也就是说 mySwitch的UIControlEventValueChanged事件发生后
		 就会调用 self里面的 switchChanged:函数
		 
		 UIControlEventValueChanged就是控件值改变:
		 UISwitch来说就是 ：ON-->OFF  OFF--->ON
		 */
		
	} else {
		cell = [tableView
				dequeueReusableCellWithIdentifier:@"NewBookCell"];
		if (cell == nil) {
			cell = [[[NSBundle mainBundle] 
					 loadNibNamed:@"NewBookCell"
					owner:self options:nil] lastObject];
		}
	}
	
	
	return cell;
}

/**/
- (void) switchChanged:(id)sender
{
	UIView *sview = [(UIView *)sender superview];
	UILabel *txt = (UILabel *)[sview viewWithTag:103];
	txt.text = [NSString stringWithFormat:@"State %@", 
				[(UISwitch *)sender isOn]?@"ON":@"OFF"];
}





@end
